

/**
* 处理子字符串防止xss
* @param str
* @returns
*/
export function escapeHtml(str: string) {
 const map: {
   [key: string]:string
 } = {
   "&": "&amp;",
   "<": "&lt;",
   ">": "&gt;",
   '"': "&quot;",
   "'": "&#039;",
 };
 return str.replace(/[&<>"']/g, function (m: string) {
   return map[m];
 });
}

  /**
   * 防抖
   * @param func
   * @param wait 等待时间，毫秒
   * @param immediate 是否立即执行(先执行再等待)
   * @returns
   */
  export function debounce<T extends (...args: any[]) => void>(
    func: T,
    wait: number,
    immediate?: boolean
  ): (...args: Parameters<T>) => void {
    let timeoutId: number | null = null;
    return function (this: any, ...args: Parameters<T>) {
      const later = () => {
        timeoutId = null;
        if (!immediate) func.apply(this, args);
      };
      const shouldCallNow = immediate && timeoutId === null;
      if (timeoutId !== null) {
        clearTimeout(timeoutId);
      }
      timeoutId = setTimeout(later, wait);
      if (shouldCallNow) {
        func.apply(this, args);
      }
    };
  }

    /**
   * 获取一个uid
   * @param ln uid长度
   * @returns uid
   */
    export function getUid(ln = 14): string {
      const randStr = `1234567890-=qwertyuiop[]asdfghjklzxcvbnm,.QWERTYUIOPASDFGHJKLZXCVBNM()!@#$%^&*_+()`;
      let str = "";
      for (let i = 0; i < ln; i++) {
        const randI = Math.floor(Math.random() * randStr.length);
        str += randStr[randI];
      }
      return `_${str}_`;
    }